package com.yc.dict.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.api.CommonPage;
import com.yc.api.CommonResult;
import com.yc.base.BaseController;
import com.yc.dict.entity.Dict;
import com.yc.dict.service.IDictDetailService;
import com.yc.dict.service.IDictService;
import com.yc.dict.vo.DictDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台字典表 前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-12-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/dict")
@Api(value = "DictController", tags = "后台字典表")
public class DictController extends BaseController {

    private final IDictService dictService;
    private final IDictDetailService dictDetailService;

    @PostMapping("/add")
    @SaCheckPermission(value = {"administrator", "management:dict:add"}, mode = SaMode.OR)
    @ApiOperation(value = "创建Dict", notes = "创建Dict")
    public CommonResult<?> create(@RequestBody Dict dict) {
        boolean isOk = dictService.create(dict);
        if (isOk) {
            return CommonResult.success(dict);
        }
        return CommonResult.failed();
    }


    @PostMapping("/deleteByIds")
    @SaCheckPermission(value = {"administrator", "management:dict:delete"}, mode = SaMode.OR)
    @ApiOperation(value = "删除Dict", notes = "删除Dict")
    public CommonResult<?> deleteByIds(@RequestBody List<Long> ids) {
        boolean isOk = dictService.delete(ids);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateById/{id}")
    @SaCheckPermission(value = {"administrator", "management:dict:update"}, mode = SaMode.OR)
    @ApiOperation(value = "更新Dict", notes = "更新Dict")
    public CommonResult<?> updateById(@PathVariable("id") long id, @RequestBody Dict dict) {
        dict.setId(id);
        boolean isOk = dictService.update(dict);
        if (isOk) {
            return CommonResult.success(dict);
        }
        return CommonResult.failed();
    }

    @PostMapping("/queryById/{id}")
    @ApiOperation(value = "根据Id获取Dict", notes = "根据Id获取Dict")
    public CommonResult<?> queryById(@PathVariable("id") Long id) {
        Dict dict = dictService.queryById(id);
        if (dict != null) {
            return CommonResult.success(dict);
        }
        return CommonResult.failed();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取Dict列表", notes = "获取Dict列表")
    public CommonResult<?> listAll() {
        List<Dict> list = dictService.selectList();
        return CommonResult.success(list);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取Dict列表", notes = "获取Dict列表")
    public CommonResult<Object> pageAll(@RequestBody JSONObject params) {
        int pageNum = params.getInt("pageNum", 1);
        int pageSize = params.getInt("pageSize", 25);
        IPage<Dict> page = dictService.selectPage(new Page<>(pageNum, pageSize));
        return CommonResult.success(CommonPage.restPage(page));
    }

    @PostMapping("/queryDetailsByName/{name}")
    @ApiOperation(value = "根据name获取DictDetails", notes = "根据name获取DictDetails")
    public CommonResult<?> queryDetailsByName(@PathVariable("name") String name) {
        List<DictDetailVo> dictDetailList = dictDetailService.queryByDictName(name);
        return CommonResult.success(dictDetailList);
    }
}
